<template>
    <div>
        <el-form ref="elForm" :model="formData" :rules="rule" size="medium" label-width="100px">
            <el-form-item label="用户账号" prop="account">
                <el-input v-model="formData.account" placeholder="请输入用户账号" :disabled="true" :style="{ width: '100%' }"></el-input>
            </el-form-item>
            <el-form-item label="真实姓名" prop="uName">
                <el-input v-model="formData.uName" placeholder="请输入单行文本真实姓名" clearable :style="{ width: '100%' }"> </el-input>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button type="primary" @click="UptUser">修改信息</el-button>
        </div>
        <div>
            <el-form ref="elForm" :model="PasswordData" :rules="rules" size="medium" label-width="100px">
                <el-form-item label="原密码" prop="Password">
                    <el-input v-model="PasswordData.Password" placeholder="请单行文本入输原密码" :style="{ width: '100%' }"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="Password1">
                    <el-input v-model="PasswordData.Password1" placeholder="请输入新密码" show-password :style="{ width: '100%' }">
                    </el-input>
                </el-form-item>
                <el-form-item label="确定密码" prop="Password2">
                    <el-input v-model="PasswordData.Password2" placeholder="请输入确定密码" show-password :style="{ width: '100%' }">
                    </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button type="primary" @click="UptPassWord">修改密码</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { UserFind, PasswordUpt, updateUser } from '@/api/user';

export default {
    components: {},
    props: [],
    data() {
        return {
            //用户信息
            formData: {
                account: undefined,
                uName: undefined
            },
            //密码修改
            PasswordData: {
                Password: undefined,
                Password1: undefined,
                Password2: undefined
            },
            //密码验证
            rule: {
                account: [],
                uName: [
                    {
                        required: true,
                        message: '请输入单行文本真实姓名',
                        trigger: 'blur'
                    }
                ]
            },
            rules: {
                Password: [],
                Password1: [
                    {
                        required: true,
                        message: '请输入新密码',
                        trigger: 'blur'
                    }
                ],
                Password2: [
                    {
                        required: true,
                        message: '请输入确定密码',
                        trigger: 'blur'
                    }
                ]
            },
            //修改用户信息
            Table: []
        };
    },
    computed: {},
    watch: {},
    created() {},
    mounted() {
        this.Show();
    },
    methods: {
        //用户反填信息
        Show() {
            UserFind().then((str) => {
                console.log(str.data);
                this.Table = str.data;
                this.formData.account = str.data.account;
                this.formData.uName = str.data.uName;
            });
        },
        //用户修改信息
        UptUser() {
            this.Table.uName = this.formData.uName;
            let data = {
                uid: this.Table.uId,
                uName: this.Table.uName,
                status: this.Table.status,
                depId: this.Table.depId
            };
            console.log(data);
            updateUser(data).then((str) => {
                if (str.data > 0) {
                    alert('修改成功!');
                }
                this.Show();
            });
        },
        //用户修改密码
        UptPassWord() {
            if (this.PasswordData.Password1 == this.PasswordData.Password2) {
                let data = {
                    uid: this.Table.uId,
                    password: this.PasswordData.Password1
                };
                if (this.PasswordData.Password != this.PasswordData.Password1) {
                    console.log(data);
                    PasswordUpt(data).then((str) => {
                        if (str.data > 0) {
                            alert(str.msg);
                            localStorage.removeItem('ms_username');
                            this.$router.push('/login');
                        } else {
                            alert('修改失败,原密码错误!');
                        }
                    });
                } else {
                    alert('密码与原密码相同,请重新输入!');
                }
            } else {
                alert('输入密码不相同,请重新输入!');
            }
        }
    }
};
</script>
